﻿using System.Linq.Dynamic.Core.Tokenizer;
using ZenSystemService.Api.Common;
using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.Entities;

namespace ZenSystemService.Api.Business
{
    public class BaseBusiness: BaseBusinessModel
    {
        #region init method
        private static BaseBusiness _businessModel = null;
        public static BaseBusiness Instance
        {
            get
            {
                lock (_lockObj)
                {
                    if (_businessModel == null)
                        _businessModel = new BaseBusiness();
                    return _businessModel;
                }
            }
        }

        #endregion
        static object _lockCourseNumber = new object();
        /// <summary>
        /// 生成课程编号
        /// </summary>
        /// <returns></returns>
        public string CreateCourseNumber()
        {
            lock (_lockCourseNumber)
            {
                Random random = new Random();
                int randomNumber = random.Next(1000, 9999);
                return DateTime.Now.ToString("yyyyMMddHHmmssfff") + randomNumber;
            }
        }
        public  void AutoMap(object source, object destination)
        {
            var sourceProperties = source.GetType().GetProperties();
            var destinationProperties = destination.GetType().GetProperties();

            foreach (var prop in sourceProperties)
            {
                var matchingProp = destinationProperties.FirstOrDefault(p => p.Name == prop.Name && p.PropertyType == prop.PropertyType);
                if (matchingProp != null && matchingProp.CanWrite)
                {
                    var value = prop.GetValue(source);
                    if (value == null && matchingProp.PropertyType.IsValueType)
                    {
                        // 对于值类型，需要特殊处理，例如使用默认(T)来获取默认值
                        value = Activator.CreateInstance(matchingProp.PropertyType);
                    }
                    matchingProp.SetValue(destination, value);
                }
            }
        }



        /// <summary>
        /// 加载下一级区域信息 dto
        /// </summary>
        /// <param name="AllAreaList"></param>
        /// <param name="parentid"></param>
        /// <param name="level"></param>
        /// <param name="_currentAreaDTO"></param>
        public void LoadSubAreaDTO(List<Area> AllAreaList, int parentid, int level, ref AreaDTO _currentAreaDTO)
        {
            if (level > 3)
                return;
            IList<Area> AreaList = AllAreaList.Where(item => item.ParentID == parentid).ToList();
            if (AreaList != null && AreaList.Count > 0)
            {
                _currentAreaDTO.children = new List<AreaDTO>();
                for (int i = 0; i < AreaList.Count; i++)
                {
                    AreaDTO _areaDTO = new AreaDTO();
                    _areaDTO.code = AreaList[i].Code.ToString();
                    _areaDTO.value = AreaList[i].ID.ToString();
                    _areaDTO.label = AreaList[i].Name;
                    LoadSubAreaDTO(AllAreaList, AreaList[i].ID, level+1,ref _areaDTO);
                    _currentAreaDTO.children.Add(_areaDTO);
                }
            }
        }

        /// <summary>
        /// 判断课程学员是否存在
        /// </summary>
        /// <param name="CourseTraineeList"></param>
        /// <param name="TraineeId"></param>
        /// <returns></returns>
        public bool JudgeCourseTraineeIsExist(List<CourseTrainee> CourseTraineeList, int TraineeId)
        {
            if (CourseTraineeList == null || CourseTraineeList.Count == 0)
                return false;
            for (int i = 0; i < CourseTraineeList.Count; i++)
            {
                if (CourseTraineeList[i].TraineeId == TraineeId)
                    return true;
            }
            return false;
        }


        /// <summary>
        /// 判断课程训练项是否存在
        /// </summary>
        /// <param name="CourseTraineeList"></param>
        /// <param name="TraineeId"></param>
        /// <returns></returns>
        public bool JudgeCourseTrainingItemIsExist(List<CourseTrainingItem> CourseTrainingItemList, int TrainingItemId)
        {
            if (CourseTrainingItemList == null || CourseTrainingItemList.Count == 0)
                return false;
            for (int i = 0; i < CourseTrainingItemList.Count; i++)
            {
                if (CourseTrainingItemList[i].TrainingItemId == TrainingItemId)
                    return true;
            }
            return false;
        }


        public bool ValidateUserIsExist(List<UserInfoResponse> UserInfoResponseList, int userid)
        {
            if (UserInfoResponseList == null || UserInfoResponseList.Count == 0)
                return false;
            for (int i = 0; i < UserInfoResponseList.Count; i++)
            {
                if (UserInfoResponseList[i].UserId == userid)
                    return true;
            }
            return false;
        }


        /*
        /// <summary>
        /// 创建userTocken
        /// </summary>
        /// <param name="id"></param>
        /// <param name="expires_in">单位秒</param>
        /// <returns></returns>
        public string CreateUserToken(int id, int expires_in)
        {
            string Separator = "%$123$%";
            string TokenSeparator = "%$789$%";
            string EncryStr= "@@&123&!!";
            string token = id + Separator + CommonTimeStamp.GetTimeStampSeconds(DateTime.Now.AddSeconds(expires_in));
            return token + TokenSeparator + BCrypt.Net.BCrypt.HashPassword(token+ EncryStr);
        }
        */


        /*
        /// <summary>
        /// 验证userTocken
        /// </summary>
        /// <param name="usertoken"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool ValidateUserToken(string usertoken,out int userid, out string msg)
        {
            userid = 0; msg = "";
            try
            {
                string Separator = "%$123$%";
                string TokenSeparator = "%$789$%";
                string EncryStr = "@@&123&!!";
                string[] tokenArr = usertoken.Split(new string[] { TokenSeparator }, StringSplitOptions.None);

                //明文
                string Plaintext= tokenArr[0];
                //密文
                string Ciphertext = tokenArr[1];

                if (BCrypt.Net.BCrypt.HashPassword(Plaintext + EncryStr) != Ciphertext)
                {
                    msg = "token参数错误";
                    return false;
                }

                //判断到期时间
                string[] PlaintextArr = Plaintext.Split(new string[] { Separator }, StringSplitOptions.None);
                userid = Convert.ToInt32(PlaintextArr[0]);
                string endTimeStr = PlaintextArr[1];
                DateTime endTime = CommonTimeStamp.GetDateTimeSeconds(Convert.ToInt64(endTimeStr));
                if (DateTime.Now > endTime)
                {
                    msg = "token已过期";
                    return false;
                }
                return true;
            }
            catch
            {
                msg = "解析token错误";
                return false;
            }
        }
        */

    }
}
